﻿#include "developshell.h"
#include "ui_developshell.h"


#include <QSqlError>
#include <QSqlQuery>

#include <QDebug>


DevelopShell::DevelopShell(QSqlDatabase &db, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DevelopShell),
    query(db)
{
    ui->setupUi(this);
}

DevelopShell::~DevelopShell()
{
    delete ui;
}

#include <QTime>
#include <QSqlResult>
#include <QSqlRecord>

void DevelopShell::on_buttonExcu_clicked()
{
    query.prepare(ui->editCMD->toPlainText());

    if(query.exec()){
        ui->browserDisplay->append("<p>"+QTime::currentTime().toString()+"[Input]<b><font color=#00FF00>"+query.lastQuery()+"</font></b></p>");
        ui->browserDisplay->append("<p>"+QTime::currentTime().toString()+"[Output]</p>");

        QSqlRecord rec = query.record();
        int length = rec.count(); // get the col of data

        if(length!=0) // if length is not zero then is a record data, we should output some data.
            while (query.next()) {
                QString data="";
                for(int i=0;i<length;i++){
                    data+=query.value(i).toString()+'\t';
                }
            ui->browserDisplay->append("<p><b><font color=#0000FF>"+data+"</font></b></p>");
        }else{
            ui->browserDisplay->append("<p>OK.</p>");
        }
        ui->editCMD->clear();
    }
    else{
        ui->browserDisplay->append("<p>"+QTime::currentTime().toString()+"[Error]<b><font color=#FF0000>"+query.lastError().text()+"</font></b></p>");
    }
}

